package com.webSite.demo.cy.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.webSite.demo.cy.bean.HomeMeta;
import com.webSite.demo.cy.bean.HomeNews;
import com.webSite.demo.cy.bean.HomeYeWu;
import com.webSite.demo.cy.mapper.HomeMetaMapper;
import com.webSite.demo.cy.mapper.HomeNewsMapper;
import com.webSite.demo.cy.mapper.HomeYeWuMapper;

@Service
@Transactional
public class CommonService {

	@Autowired
	HomeMetaMapper homeMetaMapper;
	@Autowired
	HomeNewsMapper homeNewsMapper;
	@Autowired
	HomeYeWuMapper homeYeWuMapper;
	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	public List<HomeMeta> findMete(String currentMenu) {
		List<HomeMeta> list = homeMetaMapper.findMeta(currentMenu);
		return list;
	}
	
	public List<HomeNews> findRandData(int id,int num){
		List<HomeNews> list = homeNewsMapper.findRandNews(id,num);
		return list;
	}
	
	/**
	 * 添加缓存
	* @Title: getYeWuOne  
	* @param @param id
	* @param @return    参数  
	* @return HomeYeWu    返回类型  
	* @throws
	 */
	@Cacheable(cacheNames="yewu")
	public HomeYeWu getYeWuOne(Integer id) {
		return homeYeWuMapper.selectById(id);
	}
	
	/**
	 * 更新缓存
	* @Title: updateDate  
	* @param @param homeYeWu
	* @param @return    参数  
	* @return HomeYeWu    返回类型  
	* @throws
	 */
	@CachePut(value="yewu",key="#homeYeWu.id")
	public HomeYeWu updateDate(HomeYeWu homeYeWu) {
		homeYeWu.setUpdated(new Date());
		homeYeWuMapper.updateAllColumnById(homeYeWu);
		return homeYeWu;
	}
	
	/**
	 * 清除缓存(allEntries清除所有缓存)
	* @Title: delYeWuOne  
	* @param @param id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@CacheEvict(value="yewu",allEntries=true)
	public String delYeWuOne(Integer id) {
		System.out.println("调用了方法删除id为 "+id);
		return "success";
	}
	
	@Async
	public void TestAsync() {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("success");
	}
	
//	@Scheduled(cron="0/1 * * * * ?")
//	public void testScheduled() {
//		System.out.println("时间测试");
//	}
	
	
}
